﻿#include "fmserviceaccess.h"

fmServiceAccess::fmServiceAccess(QObject *parent) : QObject(parent)
{
      strServer ="";
      strDESKey ="";
      nReadTime = 1;
      isGetData = false;

      nCurrentQueryType = 0;
      strCodeList.clear();
	        strOrder         = "";

      strOrderDateTime = "";
      strLinkState     = "未连接";

      nCurrentTotal    = 0;

      manager = new QNetworkAccessManager(this);  //新建QNetworkAccessManager对象
      connect(manager,SIGNAL(finished(QNetworkReply*)),this,SLOT(slot_ReplyFinished(QNetworkReply*)));//关联信号和槽
}

void fmServiceAccess::loadAccessConfig()
{

}

void fmServiceAccess::setServiceConfig(QString strAddress, QString strDESKey, uint nTime)
{
    strServer = strAddress;
    strDESKey = strDESKey;
    nReadTime = nTime;
}

void fmServiceAccess::slot_ReplyFinished(QNetworkReply *reply)
{
    isGetData = true;
   // qDebug()<<"recv  data!!";
     strLinkState     ="已连接 请求返回";
    //QString allinfo = reply->readAll();

//    QJsonParseError err;
//    QJsonDocument json_recv = QJsonDocument::fromJson(allinfo.toUtf8(),&err);//解析json对象
//    qDebug() <<"Json-Error:"<< err.error;

    if(1==nCurrentQueryType){

        slot_QueryCodeList();
    }
    else  if(2==nCurrentQueryType){

    }
    reply->deleteLater(); //销毁请求对象
    if(isGetData)
    {
       // qDebug()<<QString::fromLocal8Bit("获取成功");
        emit this->sig_DataSuccessedSignal();
    }
    else
    {
       // qDebug()<<QString::fromLocal8Bit("获取失败");
        emit this->sig_DataFailedSignal();
    }
    emit this->sig_DataFinisedSignal();
}

void fmServiceAccess::slot_Query()
{
    slot_Login();
}

void fmServiceAccess::slot_QueryCodeList()
{
    isGetData = false;
    nCurrentQueryType =2;
    QNetworkRequest quest;
    quest.setUrl(QUrl(strServer));
    quest.setHeader(QNetworkRequest::ContentLengthHeader,"RT-Thread ART");
    /*发送get网络请求*/
    manager->get(quest);
}

void fmServiceAccess::slot_Login()
{
    isGetData = false;
    nCurrentQueryType = 1;
    QNetworkRequest quest;
    quest.setUrl(QUrl(strServer));
    quest.setHeader(QNetworkRequest::ContentLengthHeader,"RT-Thread ART");
    /*发送get网络请求*/
    manager->get(quest);
}
